package org.keyroy.mapEditor.ui.views;

import java.util.Enumeration;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.wb.swt.SWTResourceManager;
import org.keyroy.map.core.Map;
import org.keyroy.map.core.overlays.ObjOverlay;
import org.keyroy.map.core.overlays.ObjOverlay.Area;
import org.keyroy.map.core.overlays.ObjOverlay.Line;
import org.keyroy.map.core.overlays.ObjOverlay.ObjGroup;
import org.keyroy.map.core.overlays.ObjOverlay.Point;

public class ObjectOverlayView extends Composite {
	private static final String TAG = "data";
	private ObjectStateChangeListener listener;
	private Tree tree;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public ObjectOverlayView(Composite parent, int style, ObjectStateChangeListener listener) {
		super(parent, style);
		this.listener = listener;
		setLayout(new FormLayout());

		tree = new Tree(this, SWT.BORDER | SWT.CHECK);
		tree.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TreeItem[] treeItems = tree.getSelection();
				if (treeItems != null && treeItems.length > 0) {
					TreeItem item = treeItems[0];
					Object object = item.getData(TAG);
					if (object instanceof ObjOverlay.ObjGroup) {
						((ObjOverlay.ObjGroup) object).isHide = (item.getChecked() == false);
						callObjectStateChange();
					} else if (object instanceof ObjOverlay.Point) {
						((ObjOverlay.Point) object).isHide = (item.getChecked() == false);
						callObjectStateChange();
					} else if (object instanceof ObjOverlay.Line) {
						((ObjOverlay.Line) object).isHide = (item.getChecked() == false);
						callObjectStateChange();
					} else if (object instanceof ObjOverlay.Area) {
						((ObjOverlay.Area) object).isHide = (item.getChecked() == false);
						callObjectStateChange();
					}
				}
			}
		});
		FormData fd_tree = new FormData();
		fd_tree.bottom = new FormAttachment(100);
		fd_tree.right = new FormAttachment(100);
		fd_tree.top = new FormAttachment(0);
		fd_tree.left = new FormAttachment(0);
		tree.setLayoutData(fd_tree);

		TreeItem trtmGroup = new TreeItem(tree, SWT.NONE);
		trtmGroup.setImage(SWTResourceManager.getImage(ObjectOverlayView.class, "/res/icons/brkp_grp.gif"));
		trtmGroup.setText("group");

		TreeItem trtmPoint = new TreeItem(trtmGroup, SWT.NONE);
		trtmPoint.setImage(SWTResourceManager.getImage(ObjectOverlayView.class, "/res/icons/targetpublic_obj.gif"));
		trtmPoint.setText("point");

		TreeItem trtmLine = new TreeItem(trtmGroup, SWT.NONE);
		trtmLine.setImage(SWTResourceManager.getImage(ObjectOverlayView.class, "/res/icons/disconnect_co(1).gif"));
		trtmLine.setText("line");

		TreeItem trtmNewTreeitem = new TreeItem(trtmGroup, SWT.NONE);
		trtmNewTreeitem.setImage(SWTResourceManager.getImage(ObjectOverlayView.class, "/res/icons/empty_pack_obj.gif"));
		trtmNewTreeitem.setText("area");
		trtmNewTreeitem.setExpanded(true);
		trtmGroup.setExpanded(true);

	}

	public void flushMap(Map map) {
		tree.removeAll();
		if (map.objOverlay != null) {
			Enumeration<ObjGroup> groups = map.objOverlay.groups.elements();
			while (groups.hasMoreElements()) {
				ObjOverlay.ObjGroup objGroup = (ObjOverlay.ObjGroup) groups.nextElement();
				TreeItem trtmGroup = addGroup(objGroup);

				List<ObjOverlay.Point> points = map.objOverlay.getPoints();
				for (Point point : points) {
					addPoint(trtmGroup, point);
				}

				List<ObjOverlay.Line> lines = map.objOverlay.getLines();
				for (Line line : lines) {
					addLine(trtmGroup, line);
				}

				List<ObjOverlay.Area> areas = map.objOverlay.getAreas();
				for (Area area : areas) {
					addArea(trtmGroup, area);
				}
			}
			if (map.objOverlay.groups.size() > 0) {
				tree.layout();
			}
		}
	}

	public TreeItem addGroup(final ObjOverlay.ObjGroup group) {
		TreeItem trtmGroup = new TreeItem(tree, SWT.NONE);
		trtmGroup.setImage(SWTResourceManager.getImage(ObjectOverlayView.class, "/res/icons/brkp_grp.gif"));
		trtmGroup.setText(group.name);
		trtmGroup.setData(TAG, group);
		return trtmGroup;
	}

	private final void addPoint(TreeItem trtmGroup, final ObjOverlay.Point pointObject) {
		final TreeItem trtmPoint = new TreeItem(trtmGroup, SWT.NONE);
		trtmPoint.setChecked(pointObject.isHide == false);
		trtmPoint.setImage(SWTResourceManager.getImage(ShowConfigView.class, "/res/icons/targetpublic_obj.gif"));
		trtmPoint.setText(pointObject.name);
		trtmGroup.setData(TAG, pointObject);
	}

	private final void addLine(TreeItem trtmGroup, ObjOverlay.Line lineObject) {
		final TreeItem trtmPoint = new TreeItem(trtmGroup, SWT.NONE);
		trtmPoint.setChecked(lineObject.isHide == false);
		trtmPoint.setImage(SWTResourceManager.getImage(ShowConfigView.class, "/res/icons/disconnect_co(1).gif"));
		trtmPoint.setText(lineObject.name);
		trtmGroup.setData(TAG, lineObject);
	}

	private final void addArea(TreeItem trtmGroup, ObjOverlay.Area areaObject) {
		final TreeItem trtmPoint = new TreeItem(trtmGroup, SWT.NONE);
		trtmPoint.setChecked(areaObject.isHide == false);
		trtmPoint.setImage(SWTResourceManager
				.getImage(ShowConfigView.class, "/res/icons/empty_logical_package_obj.gif"));
		trtmPoint.setText(areaObject.name);
		trtmGroup.setData(TAG, areaObject);
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	private final void callObjectStateChange() {
		if (listener != null) {
			listener.onObjectStateChange();
		}
	}

	public static interface ObjectStateChangeListener {
		public void onObjectStateChange();
	}

	public Tree getTree() {
		return tree;
	}
}
